//
//  ViewController.m
//  ZH_Helper
//
//  Created by caixiang on 2017/10/14.
//  Copyright © 2017年 蔡翔. All rights reserved.
//

#import "ViewController.h"
#import "ZHShowHUD.h"
typedef enum : NSUInteger {
    CASE_1, // 显示文本和菊花,延时3秒后消失
    CASE_2, // 仅仅显示文本,延时3秒后消失
    CASE_3, // 加载自定义view,3秒后消失
} E_CASE;
@interface ViewController ()

@property (nonatomic, assign) NSInteger  caseType;

@end

@implementation ViewController

- (void)showHUD
{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    
    switch (_caseType ++%3) {
            
            
        case CASE_1: {
           ZHShowHUD *hud = [ZHShowHUD showText:@"去你大爷的" configParameter:^(ZHShowHUD *config) {
                config.margin          = 10.f;    // 边缘留白
                config.opacity         = 0.9f;    // 设定透明度
                config.cornerRadius    = 1.f;     // 设定圆角
                config.textFont        = [UIFont systemFontOfSize:11.f];
            } inView:window];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [hud hide];
            });
        } break;
        case CASE_2: {
           ZHShowHUD *hud = [ZHShowHUD showTextOnly:@"你好吗你好吗你好吗你好吗你好吗你好吗你好吗？" configParameter:^(ZHShowHUD *config) {
                config.animationStyle  = ZHMBProgressHUDAnimationZoomOut;  // 设置动画方式
                config.margin          = 8.0f;     // 边缘留白
                config.opacity         = 0.8f;     // 设定透明度
                config.cornerRadius    = 10.0f;     // 设定圆角
                config.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];  // 设置背景色
                config.labelColor      = [[UIColor whiteColor] colorWithAlphaComponent:1.0];// 设置文本颜色
            } inView:window];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [hud hide];
            });
        } break;
            
        case CASE_3: {
            [ZHShowHUD showCustomView:^UIView *{
                // 设置自定义视图
                UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage new]];
                UIView *circle = [self getCircle];
                circle.center = imageView.center;
                [imageView addSubview:circle];
                return imageView;
            } configParameter:^(ZHShowHUD *config) {
                config.animationStyle  = ZHMBProgressHUDAnimationZoomOut;   // 设定动画方式
                config.margin          = 50.f;   // 边缘留白
                config.cornerRadius    = 5.f;    // 边缘圆角
                config.backgroundColor = [UIColor greenColor];
            } duration:4.0f inView:self.view];
        } break;
            
        default:
            break;
    }
}
- (UIView *)getCircle {
    UIView *radiusView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    radiusView.backgroundColor=[UIColor clearColor];
    UIBezierPath *beizPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(9, 9) radius:9 startAngle:-M_PI_2 endAngle:(-M_PI_2 - 0.00001) clockwise:YES];
    //先画一个圆
    CAShapeLayer *layer=[CAShapeLayer layer];
    layer.path=beizPath.CGPath;
    layer.fillColor=[UIColor clearColor].CGColor;//填充色
    layer.strokeColor=[UIColor colorWithRed:0.145 green:0.804 blue:0.992 alpha:1.000].CGColor;//边框颜色
    layer.lineWidth=2.0f;
    layer.lineCap=kCALineCapRound;//线框类型
    [radiusView.layer addSublayer:layer];
    /// 起点动画
    CABasicAnimation * strokeStartAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    strokeStartAnimation.fromValue = @(-1);
    strokeStartAnimation.toValue = @(1.0);
    /// 终点动画
    CABasicAnimation * strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    strokeEndAnimation.fromValue = @(0.0);
    strokeEndAnimation.toValue = @(1.0);
    /// 组合动画
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[strokeStartAnimation, strokeEndAnimation];
    animationGroup.duration = 0.8;
    animationGroup.repeatCount = CGFLOAT_MAX;
    animationGroup.fillMode = kCAFillModeForwards;
    animationGroup.removedOnCompletion = NO;
    [layer addAnimation:animationGroup forKey:nil];
    return radiusView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _caseType = 0;
    
    UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:button];
    [button addTarget:self
               action:@selector(buttonEvent:)
     forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonEvent:(id)sender
{
    [self showHUD];
}

@end
